home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample PMSAM / PMSAM Framework / RoboSamSlot / MSAMSlot.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  12.0 KB  |  572 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MSAMSlot.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Tim Harnett
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <18>     2/21/95    TMH        metrowerks compatiblity changes
  13.         <17>     2/16/95    TMH        got rid of ProcessIncomingMail
  14.         <16>     2/15/95    TMH        removed assert on when slotID attribute is missing
  15.         <15>     2/14/95    CL        CAttribute & CRecord I/F changes
  16.         <14>     2/14/95    TMH        replace GetEWorldSlot and GetHFSSlot with GetExternalSlot
  17.         <13>     2/10/95    TMH        change wireless to HFS
  18.         <13>     2/10/95    TMH        change wireless to HFS
  19.         <12>    12/20/94    TMH        added IsLocationActive(), Busy()
  20.         <11>    12/16/94    TMH        added GetOutTrayLetterInfo(), IsLetterDone·
  21.         <10>    12/12/94    TMH        modified so slot Id is written only once
  22.          <9>    10/18/94    TMH        read the users name and password
  23.          <8>    10/13/94    TMH        added IsOurRecipient
  24.          <7>    10/11/94    TMH        CommThread integration
  25.          <6>     10/6/94    TMH        move LogError and GetLogEntry to LogErrors.cp
  26.          <5>     10/3/94    TMH        break of THFS&TEWorld from TExternalSlot
  27.          <4>     9/22/94    TMH        fix bug where queues not avail when creating slot, use
  28.                                     GetOutQueueRef() rather than using fOutQRef directly
  29.          <3>     9/22/94    TMH        added use of CQueueEnumerator.cp,.h
  30.          <2>     9/21/94    TMH        Use new Debug.cp ASSERT stuff
  31.          <1>     9/20/94    TMH        Abandon RoadsideRest embrace Mercury
  32.          <3>     9/20/94    TMH        bug fix in TMSAMSlot::GetOutgoingLetter(), unintended call to
  33.                                     toolbox async
  34.          <2>      9/9/94    TMH        TIncoming/TOutgoing adaptations
  35.                  4/12/94    TMH        xxx put comment here xxx
  36.  
  37.     To Do:
  38. */
  39.  
  40. #ifndef __RESOURCES__
  41. #include "Resources.h"
  42. #endif
  43.  
  44. #ifndef __OCEERRORS__
  45. #include "OCEErrors.h"
  46. #endif
  47.  
  48. #ifndef __Globals__
  49. #include "Globals.h"
  50. #endif
  51.  
  52. #ifndef __Application__
  53. #include "Application.h"
  54. #endif
  55.  
  56. #ifndef __Debug__
  57. #include "Debug.h"
  58. #endif
  59.  
  60. #ifndef __SetupInfoDefinitions__
  61. //#include "SetupInfoDefinitions.h"
  62. #endif
  63.  
  64. #ifndef __MSAMSlot__
  65. #include "MSAMSlot.h"
  66. #endif
  67.  
  68. #ifndef __EWorldSlot__
  69. //#include "EWorldSlot.h"
  70. #endif
  71.  
  72. #ifndef __ExternalSlot__
  73. #include "ExternalSlot.h"
  74. #endif
  75.  
  76. #ifndef __CAttribute__
  77. #include "CAttribute.h"
  78. #endif
  79.  
  80. #ifndef __CDSSpec__
  81. #include "CDSSpec.h"
  82. #endif
  83.  
  84. #ifndef __CQueueEnumerator__
  85. #include "CQueueEnumerator.h"
  86. #endif
  87.  
  88. #ifndef __FileUtils__
  89. #include "FileUtils.h"
  90. #endif
  91.  
  92.  
  93. #ifndef __ALetter__
  94. #include "ALetter.h"
  95. #endif
  96.  
  97. #ifndef __XLetter__
  98. #include "XLetter.h"
  99. #endif
  100.  
  101. #ifndef __UFAILURE__
  102. #include "UFailure.h"
  103. #endif
  104.  
  105.  
  106. #ifndef __LogErrors__
  107. #include "LogErrors.h"
  108. #endif
  109.  
  110. #ifndef __SlotThread__
  111. #include "SlotThread.h"
  112. #endif
  113.  
  114.  
  115. //--------------------------------------
  116. //        T M S A M S l o t 
  117. //--------------------------------------
  118.  
  119.  
  120. #pragma segment MSAMSlot
  121.  
  122. //-------------------------------------------------------------------------------------
  123. TMSAMSlot::TMSAMSlot()
  124. {
  125.     fSlotRecord = 0;
  126.     fSlotID = 0;
  127.     fDirectoryRecord = 0;
  128. //    fDirDiscriminator = gZeroDirDiscriminator;
  129.     fDirDiscriminator.signature =  fDirDiscriminator.misc = 0;
  130.  
  131.  
  132.     fInQueueRef = 0;
  133.     fOutQueueRef = 0;;
  134.  
  135.     memset(&fStdSlotInfo,0,sizeof(MailStandardSlotInfoAttribute));
  136.     
  137.     fSlotThread = 0;
  138.  
  139.     fExternalSlot = 0;
  140.  
  141.  
  142. }
  143.  
  144.  
  145. //-------------------------------------------------------------------------------------
  146. TMSAMSlot::~TMSAMSlot()
  147. {
  148.     if( fDirectoryRecord != 0 ) {
  149.         delete    fDirectoryRecord;
  150.         fDirectoryRecord = 0;
  151.     }
  152.         
  153.     if( fSlotRecord != 0 ) {
  154.         delete    fSlotRecord;
  155.         fSlotRecord = 0;
  156.     }
  157.         
  158.     if( fExternalSlot != 0 ) {
  159.         delete fExternalSlot;
  160.         fExternalSlot = 0;
  161.     }
  162.     
  163.     if( fSlotThread != 0 ) {
  164.         delete fSlotThread;
  165.         fSlotThread = 0;
  166.     }
  167.     
  168.  
  169. }
  170.  
  171.  
  172. //-------------------------------------------------------------------------------------
  173. short TMSAMSlot::GetSlotID()
  174. {
  175.     if( fSlotID == 0 && fSlotRecord != 0 ) {
  176.     
  177.         CAttribute    slotIDAttr(kSlotIDAttrTypeNum,'bnry');
  178.         long slotIDsize = sizeof(short);
  179.         short slotID = 0;
  180.         OSErr osErr = slotIDAttr.Read1Value(*fSlotRecord,&slotID,&slotIDsize, gIdentity);
  181.         if( osErr == 0 && slotIDsize == 2 ) 
  182.             fSlotID = slotID;
  183.  
  184.         //•• failures
  185.     }
  186.  
  187.     return fSlotID;
  188. }
  189.  
  190.  
  191.  
  192. //-------------------------------------------------------------------------------------
  193. OSErr TMSAMSlot::Activate()
  194. {
  195.  
  196.     return    this->OpenQueues();
  197. }
  198.  
  199.  
  200. //-------------------------------------------------------------------------------------
  201. OSErr TMSAMSlot::OpenQueues()
  202. {
  203.     short slotID = this->GetSlotID();
  204.     OSErr osErr = 0;
  205.     
  206.     if( slotID != 0  ) {
  207.     
  208.         MSAMParam    pb;
  209.         memset(&pb,0,sizeof(MSAMParam));
  210.         pb.pmsamOpenQueues.msamSlotID = fSlotID;
  211.         
  212.         osErr = PMSAMOpenQueues(&pb);
  213.         if( osErr == 0 ) {
  214.             fInQueueRef = pb.pmsamOpenQueues.inQueueRef;
  215.             fOutQueueRef = pb.pmsamOpenQueues.outQueueRef;
  216.         }
  217.  
  218.     }
  219.  
  220.     return osErr;
  221. }
  222.  
  223.  
  224.  
  225. //-------------------------------------------------------------------------------------
  226. MSAMQueueRef TMSAMSlot::GetOutQueueRef()
  227. {
  228.     if( fOutQueueRef == 0 )
  229.         this->OpenQueues();
  230.  
  231.     ASSERT(fOutQueueRef!=0);
  232.  
  233.     return fOutQueueRef;
  234. }
  235.  
  236. //-------------------------------------------------------------------------------------
  237. MSAMQueueRef TMSAMSlot::GetInQueueRef()
  238. {
  239.     if( fInQueueRef == 0 )
  240.         this->OpenQueues();
  241.         
  242.     ASSERT(fInQueueRef!=0);
  243.     
  244.     return fInQueueRef;
  245.  
  246. }
  247.  
  248.  
  249.  
  250. //-------------------------------------------------------------------------------------
  251. void TMSAMSlot::IMSAMSlot(CreationID* slotCID)
  252. {
  253.  
  254.         //    Assumes the slot has been "Created" ie. all of its neccessary 
  255.         //    attributes are available in the keychain.
  256.         
  257.         
  258.     OSErr osErr = 0;
  259.     
  260.     
  261.     FailInfo fi;
  262.     Try(fi) {
  263.  
  264.  
  265.         fSlotRecord = new CRecordID(gKeyChainDSRefnum,*slotCID);    
  266.         osErr = fSlotRecord->GetNameAndType();
  267.         ASSERTNOERR(osErr);
  268.         FailOSErr(osErr);
  269.         
  270.         
  271.             // Slot id
  272.  
  273.  
  274.         CAttribute    theAttr(kSlotIDAttrTypeNum);
  275.         osErr = theAttr.Read1Value(*fSlotRecord,&fSlotID,sizeof(short), gIdentity);
  276.         if( osErr == kOCENoSuchAttributeValue ) {
  277.             fSlotID = -1;        // we are in the process of creating the slot.
  278.             osErr = 0;
  279.         }
  280.         ASSERTNOERR(osErr);
  281.         FailOSErr(osErr);
  282.  
  283.  
  284.         //    Get Directory Info
  285.         
  286.         
  287.         osErr = this->ReadDirectoryInfo();
  288.         ASSERTNOERR(osErr);
  289.         FailOSErr(osErr);
  290.         
  291.         
  292.             //    The HFS Slot
  293.             
  294.             
  295.         TExternalSlot* extSlot = new TExternalSlot;
  296.         extSlot->IExternalSlot(this);
  297.         fExternalSlot = extSlot;
  298.  
  299.  
  300.             //    A Thread For us.
  301.             
  302.             
  303.         TSlotThread* slotThread = new TSlotThread();
  304.         slotThread->ISlotThread(this);
  305.         fSlotThread = slotThread;
  306.         
  307.  
  308.         fi.Success();
  309.         
  310.     } else {
  311.     
  312.         delete this;
  313.         fi.ReSignal();
  314.         
  315.     }
  316.         
  317. }
  318.  
  319.  
  320. //-------------------------------------------------------------------------------------
  321. OSErr TMSAMSlot::ReadDirectoryInfo()
  322. {
  323.  
  324.  
  325.         //    Our Directory Record
  326.         
  327.         
  328.     #define kKeyChainMaxPackedRIDLen 200
  329.     char    packedRID[kKeyChainMaxPackedRIDLen];
  330.  
  331.  
  332.     CAttribute assocDir(kAssoDirectoryAttrTypeNum);
  333.     OSErr osErr = assocDir.Read1Value(*fSlotRecord,&packedRID,kKeyChainMaxPackedRIDLen, gIdentity);
  334.     ASSERTNOERR(osErr);
  335.     if( osErr != 0 )
  336.         return errMSAMSetupInfoCorrupt;
  337.  
  338.  
  339.     fDirectoryRecord = new CRecordID(gKeyChainDSRefnum,(PackedRecordID*)packedRID); 
  340.  
  341.  
  342.     osErr = fDirectoryRecord->GetNameAndType();
  343.     ASSERTNOERR(osErr);
  344.     if( osErr != 0 )
  345.         return errMSAMSetupInfoCorrupt;
  346.  
  347.  
  348.         //    Our Discriminator
  349.         
  350.         
  351.     CAttribute discAttr(kDiscriminatorAttrTypeNum);
  352.     osErr = discAttr.Read1Value(*fDirectoryRecord,&fDirDiscriminator,sizeof(DirDiscriminator), gIdentity);
  353.     ASSERTNOERR(osErr);
  354.     
  355.     if( osErr == 0 )
  356.         osErr = this->ReadExSysUserNameAndPassword();
  357.  
  358.     if( osErr != 0 )
  359.         osErr = errMSAMSetupInfoCorrupt;
  360.  
  361.     return osErr;
  362. }
  363.  
  364.  
  365. //-------------------------------------------------------------------------------------
  366. char* TMSAMSlot::GetExSysPassword(char* password)
  367.  
  368.     //    Refresh the cached values. They may have changed.
  369.     
  370.     this->ReadExSysUserNameAndPassword();
  371.     
  372.     fPassword.CopyBody(password);
  373.     password[fPassword.BodyLength()] = 0;
  374.     return password;
  375. }
  376.  
  377.  
  378.  
  379. //-------------------------------------------------------------------------------------
  380. char* TMSAMSlot::GetExSysUserName(char* userName)
  381. {
  382.     //    Refresh the cached values. They may have changed.
  383.  
  384.     this->ReadExSysUserNameAndPassword();
  385.     
  386.     fUserName.CopyBody(userName);
  387.     userName[fUserName.BodyLength()] = 0;
  388.     return userName;
  389.     
  390. }
  391.  
  392.  
  393.  
  394. //-------------------------------------------------------------------------------------
  395. OSErr TMSAMSlot::ReadExSysUserNameAndPassword()
  396. {    
  397.  
  398.  
  399.     OCESetupGetDirectoryInfoPB dirInfoPB;
  400.     memset(&dirInfoPB,0,sizeof(OCESetupGetDirectoryInfoPB));
  401.  
  402.     CRecordID    userRec;
  403.     CRecordName    nativeName;        // don't care about this
  404.     
  405.     dirInfoPB.dsRefNum = gKeyChainDSRefnum;
  406.     dirInfoPB.identity = gIdentity;
  407.  
  408.     CDirectoryName    dirName(this->GetDirectoryName());
  409.     dirInfoPB.directoryName = dirName;
  410.     dirInfoPB.discriminator = fDirDiscriminator;
  411.     
  412.     dirInfoPB.recordID = userRec;
  413.     dirInfoPB.nativeName = (RStringPtr)&nativeName;
  414.     dirInfoPB.password = (RStringPtr)&fPassword;        // we cache this
  415.     
  416.     
  417.     OSErr osErr = OCESetupGetDirectoryInfo((AuthParamBlockPtr)&dirInfoPB,false);
  418.  
  419.     if( osErr == 0 ) 
  420.         fUserName = userRec.Name();        // ... and this 
  421.  
  422.     osErr = 0;  //••• testing
  423.     return osErr;
  424. }
  425.  
  426.  
  427.  
  428. //-------------------------------------------------------------------------------------
  429. Boolean    TMSAMSlot::IsOurRecipient(CUnpackedDSSpec& recipient)
  430. {
  431.  
  432.     if( recipient.fExtType != fDirDiscriminator.signature )
  433.         return false;
  434.     
  435.  
  436.     return recipient.DirName() == (CDirectoryName&) fDirectoryRecord->Name();
  437.         
  438.  
  439.  
  440. }
  441.  
  442.  
  443. //-------------------------------------------------------------------------------------
  444. OSErr TMSAMSlot::SetSlotInfo()
  445. {
  446.     
  447.     // Get standard slot information
  448.     
  449.     CAttribute    theAttr(kStdSlotInfoAttrTypeNum);
  450.     OSErr osErr = theAttr.Read1Value(*fSlotRecord,&fStdSlotInfo,sizeof(MailStandardSlotInfoAttribute), gIdentity);
  451.     ASSERTNOERR(osErr);
  452.     FailOSErr(osErr);
  453.     
  454.     
  455.     //    ...TBD: there is more config info to get here.
  456.  
  457.  
  458.     //    Configure the external slot --
  459.     if( fExternalSlot != 0 )
  460.         fExternalSlot->SetConfiguration();
  461.     
  462.     
  463.     return osErr;
  464. }
  465.  
  466.  
  467.  
  468. #pragma segment MSAMSlot
  469. //-----------------------------------------------------------------------------------
  470. TOutgoingALetter* TMSAMSlot::GetOutgoingLetter()
  471. {
  472.  
  473.     TOutgoingALetter* outgoingLetter = 0;
  474.     COutQEnumerator iter( this->GetOutQueueRef() );
  475.     
  476.     for(MSAMEnumerateOutQReply* enumReply = iter.FirstLetter(); iter.More(); enumReply=iter.NextLetter()) {
  477.     
  478.         if( !enumReply->done ) {
  479.             outgoingLetter = new TOutgoingALetter;
  480.             outgoingLetter->IOutgoingALetter(this,enumReply);
  481.             break;
  482.         }
  483.         
  484.     }
  485.     
  486.     return outgoingLetter;
  487. }
  488.  
  489. //-----------------------------------------------------------------------------------
  490. long TMSAMSlot::CountOutgoingLetters()
  491. {
  492.  
  493.     long    noutgoingLetters = 0;
  494.     COutQEnumerator iter( this->GetOutQueueRef() );
  495.     for(MSAMEnumerateOutQReply* enumReply = iter.FirstLetter(); iter.More(); enumReply=iter.NextLetter())
  496.         if( !enumReply->done )
  497.             noutgoingLetters++;
  498.  
  499.     return noutgoingLetters;
  500.     
  501. }
  502.  
  503.  
  504. //-----------------------------------------------------------------------------------
  505. OSErr TMSAMSlot::GetOutTrayLetterInfo(long letterSeqNo,MSAMEnumerateOutQReply& outQReply)
  506. {
  507.     MSAMEnumeratePB     pb;
  508.     EnumOutQReply         enumBlock;
  509.  
  510.     pb.ioCompletion = 0;
  511.     pb.queueRef  = this->GetOutQueueRef();
  512.     pb.startSeqNum = letterSeqNo;
  513.     pb.buffer.bufferSize = sizeof(EnumOutQReply);
  514.     pb.buffer.buffer = (Ptr)&enumBlock;
  515.  
  516.     MSAMEnumerate((MSAMParam *)&pb,true);
  517.     do {
  518.         gCurrentThread->Yield();
  519.     } while( pb.ioResult > 0 );
  520.  
  521.  
  522.     ASSERTNOERR(pb.ioResult);
  523.     if( pb.ioResult == 0 )
  524.         BlockMoveData(&enumBlock.outQEnumReply,&outQReply,sizeof(MSAMEnumerateOutQReply));
  525.         
  526.     return pb.ioResult;
  527.  
  528. }
  529.  
  530.  
  531. //-----------------------------------------------------------------------------------
  532. Boolean TMSAMSlot::IsLetterDone(long letterSeqNo)
  533. {
  534.  
  535.     Boolean    isDone = true;
  536.     
  537.     MSAMEnumerateOutQReply    outTrayLetterInfo;
  538.     OSErr osErr = this->GetOutTrayLetterInfo(letterSeqNo,outTrayLetterInfo);
  539.     if( osErr == 0 )
  540.         isDone = outTrayLetterInfo.done;
  541.         
  542.     return isDone;
  543.  
  544. }
  545.  
  546.  
  547. //-----------------------------------------------------------------------------------
  548. Boolean    TMSAMSlot::Busy()
  549. {
  550.     return (fSlotThread != 0) && (fSlotThread->GetThreadState() != kStoppedThreadState);
  551. }
  552.  
  553.  
  554. //-----------------------------------------------------------------------------------
  555. Boolean TMSAMSlot::IsLocationEnabled()
  556. {
  557.     Boolean enabled = false;
  558.     
  559.     MailLocationFlags    ourLocation =  gApplication->Location();
  560.     
  561.     return ( fStdSlotInfo.active & MailLocationMask(ourLocation) ) != 0;
  562.  
  563. }
  564.  
  565.  
  566. //-----------------------------------------------------------------------------------
  567. TExternalSlot*    TMSAMSlot::GetExternalSlot()
  568. {  
  569.     return (TExternalSlot*) fExternalSlot;
  570. }
  571.